﻿using Microsoft.Data.SqlClient;
using HRMSystemShi.DAL;
using HRMSystemShi.Model;
using System.Data;
namespace HRMSystemShi._2025
{
    public partial class FormEmployee : Form
    {
        public FormEmployee()
        {
            InitializeComponent();
            textBoxName.Enabled = false;
            comboBoxDepartment.Enabled = false;
            dateTimePickerBegin.Enabled = false;
            dateTimePickerLast.Enabled = false;
            buttonSearch.Enabled = false;

            comboBoxDepartment.DataSource = DepartmentService.GetDepartmentList();
            comboBoxDepartment.DisplayMember = "Name";
            comboBoxDepartment.ValueMember = "Id";
            comboBoxDepartment.SelectedIndex = -1;

            buttonShowAll_Click(null!, null!);
        }
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            Employee serchEmployee = new Employee();
            if (checkBoxName.Checked)
            {
                if (string.IsNullOrEmpty(textBoxName.Text))
                {
                    CommonHelper.MessageError("请输入用户名");
                }
                else serchEmployee.Name = textBoxName.Text;
            }
            if (checkBoxDepartment.Checked)
            {
                if (comboBoxDepartment.SelectedIndex == -1)
                {
                    CommonHelper.MessageError("请选择部门");
                }
                else serchEmployee.DepartmentId = (Guid)comboBoxDepartment.SelectedValue!;
            }
            if (checkBoxInDay.Checked)
            {
                serchEmployee.InDayFrom = dateTimePickerBegin.Value;
                serchEmployee.InDayTo = dateTimePickerLast.Value;
            }
            dataGridViewEmployeeList.DataSource = EmployeeService.GetEmployeeList(serchEmployee);
        }

        private void checkBoxName_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxName.Checked)
            {
                textBoxName.Enabled = true;
            }
            else
            {
                textBoxName.Enabled = false;
            }
            buttonSearch.Enabled = checkBoxName.Checked || checkBoxDepartment.Checked || checkBoxInDay.Checked;
        }

        private void checkBoxDepartment_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxDepartment.Checked)
            {
                comboBoxDepartment.Enabled = true;
            }
            else
            {
                comboBoxDepartment.Enabled = false;
            }
            buttonSearch.Enabled = checkBoxName.Checked || checkBoxDepartment.Checked || checkBoxInDay.Checked;
        }

        private void checkBoxInDay_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxInDay.Checked)
            {
                dateTimePickerBegin.Enabled = true;
                dateTimePickerLast.Enabled = true;
            }
            else
            {
                dateTimePickerBegin.Enabled = false;
                dateTimePickerLast.Enabled = false;
            }
            buttonSearch.Enabled = checkBoxName.Checked || checkBoxDepartment.Checked || checkBoxInDay.Checked;
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            dataGridViewEmployeeList.DataSource = EmployeeService.GetEmployeeList();
        }

        private void tsbNew_Click(object sender, EventArgs e)
        {
            FormEmployeeEdit formEmployeeEdit = new FormEmployeeEdit();
            if (formEmployeeEdit.ShowDialog() == DialogResult.OK)
            {
                CommonHelper.MessageSuccess("添加成功");
            }
            else
            {
                CommonHelper.MessageError("添加失败");
            }
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dataGridViewEmployeeList.SelectedRows.Count == 0)
            {
                CommonHelper.MessageError("请选择要编辑的员工");
            }
            else
            {
                Guid id = (Guid)dataGridViewEmployeeList.SelectedRows[0].Cells["ID"].Value;
                FormEmployeeEdit formEmployeeEdit = new FormEmployeeEdit(id);
                if (formEmployeeEdit.ShowDialog() == DialogResult.OK)
                {
                    CommonHelper.MessageSuccess("修改成功");
                }
                else
                {
                    CommonHelper.MessageError("修改失败");
                }
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewEmployeeList.SelectedRows.Count == 0)
            {
                CommonHelper.MessageError("请选择正确的行!");
            }
            else
            {
                Guid id = (Guid)dataGridViewEmployeeList.SelectedRows[0].Cells["ID"].Value;
                if (CommonHelper.MessageYesOrNo("确定删除吗?") == DialogResult.Yes)
                {
                    if (EmployeeService.DeleteEmployeeById(id))
                    {
                        CommonHelper.MessageSuccess("删除成功!!");
                        buttonShowAll_Click(null!, null!);
                    }
                    else
                    {
                        CommonHelper.MessageError("删除失败，未知错误!!");
                    }
                }
                else
                {
                    CommonHelper.MessageError("取消删除");
                }
            }
        }

        private void tsbExport_Click(object sender, EventArgs e)
        {
            List<Employee> employees = new List<Employee>();
            string filePath = string.Empty;
            foreach (DataGridViewRow row in dataGridViewEmployeeList.Rows)
            {
                if (row.Cells[0].Value != null)
                {

                    Employee employee = new Employee();
                    employee.Id = (Guid)row.Cells[0].Value;
                    employee.Number = row.Cells[1].Value.ToString()!;
                    employee.Name = row.Cells[2].Value.ToString()!;
                    employee.Department = row.Cells[3].Value.ToString()!;
                    employee.InDay = (DateTime)row.Cells[4].Value;
                    employee.Nation = row.Cells[5].Value.ToString()!;
                    employee.NativePlace = row.Cells[6].Value.ToString()!;
                    employees.Add(employee);
                }
            }

            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = "xlsx文件|*.xlsx";
                saveFileDialog.Title = "导出员工列表";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = saveFileDialog.FileName;

                    if (EmployeeService.ExportEmportEXECL(employees, filePath))
                    {
                        CommonHelper.MessageSuccess("导出成功！");
                    }
                    else
                    {
                        CommonHelper.MessageError("导出失败！");
                    }
                }
                else
                {
                    CommonHelper.MessageError("导出路径错误！！");
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (CommonHelper.MessageYesOrNo("确定关闭吗?") == DialogResult.Yes)
            {
                this.Close();
            }
            else
            {
                CommonHelper.MessageError("取消操作");
            }
        }
        //public static List<Employee> ExportEmployeeDataTable(DataTable dataTable)
        //{
        //    foreach (DataGridViewRow dr in dataTable.Rows)
        //    {
        //        Employee emp = new Employee();
        //        for (int i = 0; i < dr.Table.Rows.Count; i++)
        //        {
        //        }
        //    }
        //}
    }
}
